package org.csu.mybigpro.controller;

import org.csu.mybigpro.DTO.GradingReportDto;
import org.csu.mybigpro.service.AssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/assessment")
@CrossOrigin // 临时允许跨域，生产环境应配置得更安全
public class AutoGraderController {

    @Autowired
    private AssessmentService assessmentService;

    @PostMapping("/auto-grade")
    public ResponseEntity<?> handleAutoGrading(
            @RequestParam("examFile") MultipartFile examFile,
            @RequestParam("answersFile") MultipartFile answersFile) {
        try {
            GradingReportDto report = assessmentService.gradeAndAnalyze(examFile, answersFile);
            return ResponseEntity.ok(report);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace(); // 记录错误
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("处理失败：" + e.getMessage());
        }
    }
}